﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using SharedIFaces;
using System.Data;
using System.IO;
using System.Xml;
using Data;

namespace BL
{
    // singleton-класс для обращения к серверу приложения
    public class WCF_Client
    {
        static private WCF_Client _instance = null;
        static public WCF_Client Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new WCF_Client();
                }

                return _instance;
            }
        }

        private BasicHttpBinding _binding = new BasicHttpBinding();
        private EndpointAddress _address = new EndpointAddress("http://localhost:44444/demoplayerdotnet");
        
        private IServer _channel;
        public IServer Channel
        {
            get
            {
                return _channel;
            }
        }

        private DataTable _dt_Users = new DataTable(User.TableName);
        private DataTable _dt_Songs = new DataTable(Song.TableName);
        private DataTable _dt_Playlists = new DataTable(Playlist.TableName);
        private DataTable _dt_PlaylistConnectors = new DataTable(PlaylistConnector.TableName);


        private WCF_Client()
        {
           ChannelFactory<IServer> factory = new ChannelFactory<IServer>(_binding, _address);

           _channel = factory.CreateChannel(); 
        }

        public DataTable GetTable(String table_name)
        {
            DataSet ds = new DataSet();
            String xml_string = Channel.GetTable(table_name);

            System.IO.StringReader xmlSR = new System.IO.StringReader(xml_string);
            ds.ReadXml(xmlSR, XmlReadMode.Auto);
            
            return ds.Tables[table_name];
        }

        public void UpdateTable(String table_name, DataTable content)
        {
            if (null != content)
            {
                String xml_content = content.DataSet.GetXml();

                Channel.UpdateTable(table_name,xml_content);
            }
        }

        public void InsertToTable(String table_name, DataTable content)
        {
            if (null != content)
            {
                String xml_content = content.DataSet.GetXml();

                Channel.InsertToTable(table_name, xml_content);
            }
        }

        public void DeleteFromTable(String table_name, DataTable content)
        {
            if (null != content)
            {
                String xml_content = content.DataSet.GetXml();

                Channel.DeleteFromTable(table_name, xml_content);
            }
        }

        public User CreateUser(String login, String password)
        {
            User tmp = Channel.CreateUser(login, password);
            // Добавляем полученный с сервера User в Dictionary
            User.AddToAllUsers(tmp);

            return tmp;
        }
    }
}
